<?php


namespace app\zmt\controller;


use app\zmt\model\NoticeModel;
use cmf\controller\AdminBaseController;

class NoticeController extends AdminBaseController
{
    //显示
    public function index()
    {
        //搜索
        $keyword = input('keyword/s','');
        $where = [];
        if($keyword){
            $where[] = ['title','like','%'.$keyword.'%'];
        }

        //导航列表显示
        $list = NoticeModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->where($where)
            ->paginate(5);
        $this->assign('list',$list);

        return $this->fetch('notice');
    }

    //排序
    public function px()
    {
        $nav = new NoticeModel();
        parent::listOrders($nav);
        $this->success('排序成功');
    }

    //添加页面显示
    public function add()
    {

        return $this->fetch();
    }

//添加
    public function addPost()
    {
        $data = input('post.');

        //验证器验证
//        $validata = new NavValidata();
//        $result = $validata->scene('add')->check($data);
//        if(!$result){
//            $this->error($validata->getError());
//        }
        $add = NoticeModel::create($data);

        if($add){
            $this->success('添加成功','Notice/index');
        } else {
            $this->error('添加失败');
        }
    }

    //编辑显示
    public function edit()
    {
        //接收id
        $id = input('id','');
        //编辑页面显示与数据还原
        $list = NoticeModel::find($id);

        $this->assign('list',$list);
        return $this->fetch();

    }

    //编辑
    public function editPost()
    {
        //接收编辑数据
        $data = input('post.');

        //验证器验证
//        $validata = new NavValidata();
//        $result = $validata->scene('add')->check($data);
//        if(!$result){
//            $this->error($validata->getError());
//        }

        //编辑数据逻辑
        $edit = NoticeModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }
        $edit->title = $data['title'];
        $edit->url = $data['url'];

        if($edit->save()){
            $this->success('保存成功','Notice/index');
        }else{
            $this->error('保存失败');
        }
    }
    //删除逻辑
    public function delete()
    {
        //接收删除数据id
        $id = input('id','');

        if(NoticeModel::destroy($id)){
            $this->success('删除成功','Notice/index');
        }else{
            $this->error('删除失败');
        }

    }

}